/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yutel.wuzi.view;

import com.yutel.wuzi.action.Control;
import com.yutel.wuzi.entities.ChessPad;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author yuanyajun
 */
public class Chessboard extends Canvas {

    Display display;
    Control control;
    ChessPad chessPad;

    public Chessboard(Display display) {
        this.display = display;
    }

    protected void paint(Graphics g) {
        chessPad.setGraphics(g);
        chessPad.drawChessboard();
        chessPad.drawChesses(control.getChesses());
        chessPad.drawMouse();
        control.showMessage();
    }

    public void start() {
        display.setCurrent(this);
    }

    protected void hideNotify() {
        super.hideNotify();
    }

    protected void showNotify() {
        super.showNotify();
    }

    protected void sizeChanged(int width, int height) {
        control = Control.getIntance();
        chessPad = control.getChessPad();
        chessPad.setSize(width, height);
    }

    public void pause() {
    }

    protected void keyPressed(int keyCode) {
        int action = getGameAction(keyCode);
        switch (action) {
            case LEFT:
                chessPad.getMouse().moveLeft();
                break;
            case RIGHT:
                chessPad.getMouse().moveRight();
                break;
            case UP:
                chessPad.getMouse().moveUp();
                break;
            case DOWN:
                chessPad.getMouse().moveDown();
                break;
            case FIRE:
                control.pressChess();
                break;
            case GAME_A:
                control.undoChess();
                break;
            case GAME_B:
                control.newGame();
                break;
            case GAME_C:
                setFullScreenMode(false);
                break;
            case GAME_D:
                setFullScreenMode(true);
                break;
        }
        repaint();
    }
}
